/*
 * DevicesTimer.c
 *
 *      Author: Honrun
 */
#include "stm32f1xx_hal.h"
#include "DevicesTimer.h"




/* Timer handler declaration */
TIM_HandleTypeDef   g_Tim5Handle;
TIM_HandleTypeDef   g_Tim6Handle;



int8_t cTimer5Init(void)
{
    /* Enable TIM clock */
    __HAL_RCC_TIM5_CLK_ENABLE();

    /* Initialize TIM 1us clock */
    g_Tim5Handle.Instance = TIM5;
    g_Tim5Handle.Init.Period = 1000 - 1;
    g_Tim5Handle.Init.Prescaler = (SystemCoreClock / 1000000) - 1;
    g_Tim5Handle.Init.ClockDivision = 0;
    g_Tim5Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_Base_Init(&g_Tim5Handle);

    /*Configure the TIM IRQ priority */
    HAL_NVIC_SetPriority(TIM5_IRQn, 1, 0);
    /* Enable the TIM global Interrupt */
    HAL_NVIC_EnableIRQ(TIM5_IRQn);

    /* Start the TIM time Base generation in interrupt mode */
    HAL_TIM_Base_Start_IT(&g_Tim5Handle);

    return 0;
}

int8_t cTimer6Init(void)
{
    /* Enable TIM clock */
    __HAL_RCC_TIM6_CLK_ENABLE();

    /* Initialize TIM 1us clock */
    g_Tim6Handle.Instance = TIM6;
    g_Tim6Handle.Init.Period = 65536 - 1;
    g_Tim6Handle.Init.Prescaler = (SystemCoreClock / 1000000) - 1;
    g_Tim6Handle.Init.ClockDivision = 0;
    g_Tim6Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_Base_Init(&g_Tim6Handle);

    /*Configure the TIM IRQ priority */
    HAL_NVIC_SetPriority(TIM6_IRQn, 0 ,0);
    /* Enable the TIM global Interrupt */
    HAL_NVIC_EnableIRQ(TIM6_IRQn);

    /* Start the TIM time Base generation in interrupt mode */
    HAL_TIM_Base_Start_IT(&g_Tim6Handle);

    return 0;
}
